findImageLinks();
window.addEventListener("focus",function()	{
	findImageLinks();		
})

function findImageLinks()	{
	if (window == top) {
		console.log(window.location.href+" is "+top.location.href);
		var port = chrome.extension.connect();
		var filters = [".jpg", ".png", ".gif", ".jpg.html", "/image/"];
		var imgLinks = [];
		var links = document.getElementsByTagName("a");
		for (var key in links) {
			var link = links[key];
			var linktext = link.href;
			if (linktext) {
				for (var filter in filters) {
					if (linktext.indexOf(filters[filter]) >= 0) {
						imgLinks.push(linktext);
					}
				}
			}
		}
		var imgLinks = unique(imgLinks);
		port.postMessage(imgLinks);
	}	else	{
		console.log(window.location.href+" is not "+top.location.href);
	}
}

function unique(a)
{
   var r = new Array();
   o:for(var i = 0, n = a.length; i < n; i++)
   {
      for(var x = 0, y = r.length; x < y; x++)
      {
         if(r[x]==a[i]) continue o;
      }
      r[r.length] = a[i];
   }
   return r;
}